package edu.ccut.saturn.basecomponent.database.impl;

import edu.ccut.saturn.basecomponent.database.DatabaseAttributeUtils;
import edu.ccut.saturn.basecomponent.database.IDatabaseAttribute;
import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.IUpdateSQLBuilder;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.basecomponent.database.util.SaturnSQLUtil;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.SaturnData;

public abstract class AbstractUpdateSQLBuilder extends AbstractSQLBuilder
		implements IUpdateSQLBuilder {

	public ISaturnSQL build(SaturnData saturnData, String condition,
			String specialColumn) throws Exception {
		
		IDataInfo dataInfo = saturnData.getSaturnDataInfo();
		
		StringBuffer sql = new StringBuffer();
		
		String[] specialColumns = null;
		
		if (specialColumn != null && !"".equals(specialColumn.trim())) {
			specialColumns = parseSpecialColumn(specialColumn);
		}
		
		String tableName = SaturnDataUtil.getTableName(dataInfo);
		
		sql.append("UPDATE ").append(tableName).append(" SET ");
		
		for (String key : dataInfo.keySet()) {
			
			IDataItem iDataItem = dataInfo.getDataItem(key);
			
			String colum = iDataItem.getColumn();
			String type = iDataItem.getType();
			
			// 主键不能修改
			if ("true".equalsIgnoreCase(iDataItem.getIsPrimaryKey().trim())) {
				continue;
			}
			
			if (iDataItem.getTableName() != null
					&& !"".equals(iDataItem.getTableName().trim())) {
				
				Object value = saturnData.get(key);
				
				// 同插入操作,不应判断其值是否为0&&!isZero(value)
				if (!SaturnSQLUtil.isNull(value)) {
					
					sql.append(getSetValueString(iDataItem, value)).append(",");
					
				} else if (isAcceptNull(key, specialColumns)) {
					// TODO 数字或者对象分别处理,DB2需要另行处理
					// 此处判断是必要的
					if (isNumber(type)) { // 如果是数字
						sql.append(colum).append("=0,");
					} else {
						sql.append(colum).append("=null,");
					}
				}
			}

		}
		sql.deleteCharAt(sql.length() - 1);
		sql.append(" ").append(condition);

		SaturnSQL saturnsql = new SaturnSQL();
		saturnsql.setSql(sql.toString());
		saturnsql.setDBtype(this.getDatabaseType());
		saturnsql.setSaturnData(saturnData);
		saturnsql.setSaturnDataFullKey(dataInfo.getDataFullKey());
		saturnsql.setOperationType(this.getOperationType());

		return saturnsql;
	}

	/**
	 * 数据库类型是否是数字
	 * @param type
	 * @return
	 */
	protected boolean isNumber(String type){
		IDatabaseAttribute dbAttribute = DatabaseAttributeUtils.getDatabaseAttribute(getDatabaseType());
		return dbAttribute.isNumber(type);
	}

	/**
	 * 获取SQL语句中SET后赋值的值
	 * 
	 * @param iDataItem
	 * @param value
	 * @return
	 */
	protected abstract String getSetValueString(IDataItem dataItem, Object value);

	private boolean isAcceptNull(String key, String[] specialColumnList) {
		if (specialColumnList != null) {
			for (String column : specialColumnList) {
				if (key.equals(column.trim())) {
					return true;
				}
			}
		}
		return false;
	}
	
	//引用处已经判断其是否为null了,无需再判断
	private String[] parseSpecialColumn(String parameterValue) {
			return parameterValue.split(",");
	}
	
//	protected boolean isZero(Object value) {
//		if (value instanceof Double || value instanceof Integer
//				|| value instanceof Float || value instanceof Short
//				|| value instanceof Long) {
//
//			if (value.equals(0)) {
//				return true;
//			}
//		}
//		return false;
//	}

}
